﻿using System;
using System.Collections.Generic;
using System.Text;
using WxLib;
using WxLib.Core.Util;

namespace wxLib.Lib
{
    public class WxPayApi
    {
        WxApiOptions options = new WxApiOptions();

        /**
       *    
       * 查询订单
       * @param WxPayData inputObj 提交给查询订单API的参数
       * @param int timeOut 超时时间
       * @throws WxPayException
       * @return 成功时返回订单查询结果，其他抛异常
       */
        public async System.Threading.Tasks.Task<WxPayData> OrderQueryAsync(WxPayData inputObj, int timeOut = 6)
        {
            string url = "https://api.mch.weixin.qq.com/pay/orderquery";
            //检测必填参数
            if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
            {
                return new WxPayData();
            }

            inputObj.SetValue("appid", options.AppId);//公众账号ID
            inputObj.SetValue("mch_id", options.MchId);//商户号
            inputObj.SetValue("nonce_str", Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr());//随机字符串
            inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256);//签名类型
            inputObj.SetValue("sign", inputObj.MakeSign());//签名
            string xml = inputObj.ToXml();
            var start = DateTime.Now;

            var response = await HttpRequestHelper.PostRequestAsync(new Uri(url), xml);
            var res = System.Xml.Linq.XDocument.Parse(response);

            var end = DateTime.Now;
            int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时

            //将xml格式的数据转化为对象以返回
            WxPayData result = new WxPayData();
            result.FromXml(response);

            //ReportCostTime(url, timeCost, result);//测速上报

            return result;
        }

        /// <summary>
        ///  统一下单
        /// </summary>
        /// <param name="inputObj"></param>
        /// <param name="ip">Ip</param>
        /// <param name="xcxid">小程序Id</param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task<WxPayData> UnifiedOrderAsync(WxPayData inputObj, string ip, string xcxid = "wxf0e89e07f52c745f")
        {
            string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            //异步通知url未设置，则使用配置文件中的url
            if (!inputObj.IsSet("notify_url"))
            {
                inputObj.SetValue("notify_url", "https://www.qierge.com/weixinPage/ResultNotify");//异步通知url
            }
            //如果是小程序支付，
            if (inputObj.GetValue("trade_type").ToString() == "JSAPI")
            {
                inputObj.SetValue("appid", xcxid);//小程序ID
            }
            else
            {
                inputObj.SetValue("appid", options.AppId);//公众账号ID
            }
            inputObj.SetValue("mch_id", options.MchId);//商户号
            inputObj.SetValue("spbill_create_ip", ip);//终端ip	  	    
            inputObj.SetValue("nonce_str", Senparc.Weixin.MP.TenPayLibV3.TenPayV3Util.GetNoncestr());//随机字符串
            inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256);//签名类型
            //签名
            inputObj.SetValue("sign", inputObj.MakeSign());
            string xml = inputObj.ToXml();
            var start = DateTime.Now;
            var response = await HttpRequestHelper.PostRequestAsync(new Uri(url), xml);
            WxPayData result = new WxPayData();
            result.FromXml(response);
            return result;
        }

    }
}
